
//
//  MainViewController.swift
//  MicroBlog
//
//  Created by QT on 15/7/28.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // 添加各模块的控制器
        addChildViewControllers()
        
        
        
    }
    override func viewDidAppear(animated: Bool) {
        setUpComposeBtn()
    }
    

    
    // 添加子控制器
    private func addChildViewControllers(){
        // 仅仅是添加了控制器，并没有添加tabBar的按钮，
        addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        
        /**
        下面的控制器主要用于在tabBar获得一个位置，然后将按钮覆盖到对应的tabBar上，控制器不用于显示
        */
        
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "自己", imgName: "tabbar_profile")
        
        // tabbar_message_center_highlighted   tabbar_message_center
    }
    
    
    
    // 添加单个控制器
    /**
    将模块控制器封装到导航控制器（NavCon）中,然后添加到TabBarCon中
    
    - parameter vc     : 要包装的控制器
    - parameter title  : 控制器标题
    - parameter imgName: tabBar图标
    */
    private func addChildViewController(vc: UIViewController,title: String,imgName: String) {
            // 设置标题、图片、颜色
            // application 中有设置
//            tabBar.tintColor = UIColor.orangeColor()
            vc.title = title
        
            let img = UIImage(named: imgName)
        
            // 设置使用原有色渲染
//            img = img?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
            vc.tabBarItem.image = img
        
        
        // 创建导航控制器
        let nav  = UINavigationController(rootViewController: vc)
        
        
        // 添加子控制器
        addChildViewController(nav)
        
    }

    // 懒加载”发布微博“按钮
    private lazy var composeBtn: UIButton = {
        let btn = UIButton()
        // 设置按钮背景图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 添加按钮事件
        btn.addTarget(self, action: "composeBtnClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(btn)
        
        
        
        return btn
    }()
    // 按钮点击事件
    func composeBtnClicked(){
        let vc = UserAccount.userLogon ? HMComposeViewController() : oauthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    private func setUpComposeBtn(){
        // 设置按钮位置
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.size.height)

        composeBtn.frame = CGRectOffset(rect, 2 * w, 0)

        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
